﻿using EventMgrLib;
using SmartMotion.Core;
using Sunny.UI;
using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;
using SmartMotion.Properties;
using Unity;

namespace SmartMotion
{
    public partial class FrmMain : Form
    {
        private PagesMgr _pagesMgr;
        private EventMgr _em;
        private IUnityContainer _container;

        public FrmMain()
        {
            InitializeComponent();
            
           
        }

        private void GlobalControl(object sender, EventArgs e)
        {
            var button = sender as UIHeaderButton;
            if (button == null) return;
            Controller controller = Controller.none;
            if (button == btnHome || button == btnResetAll)
            {
                controller = Controller.Home;
            }
            if (button == btnStart)
            {
                controller = Controller.Start;
            }
            if (button == btnPause)
            {
                controller = Controller.Pause;
            }
            if (button == btnContinue)
            {
                controller = Controller.Continue;
            }
            if (button == btnStop || button == btnStopAll)
            {
                controller = Controller.Stop;
            }
            if (button == btnRunRow)
            {
                controller = Controller.RunRow;
            }
            if (button == btnRunFLow)
            {
                controller = Controller.RunFlow;
            }

            _em.GetEvent<GlobalControlEvent>().Publish(controller);
        }

        private void ProductMgr(object sender, EventArgs e)
        {
            var button = sender as UIHeaderButton;
            if (button == null) return;
            var fileOpt = FileOpt.None;
            if (button == btnProductNew)
            {
                fileOpt = FileOpt.Create;
            }
            if (button == btnProductSave)
            {
                fileOpt = FileOpt.Save;
            }
            if (button == btnProductSaveAs)
            {
                fileOpt = FileOpt.SaveAs;
            }
            if (button == btnProductOpen)
            {
                fileOpt = FileOpt.Open;
            }
            if (button == btnProductAddPar)
            {
                fileOpt = FileOpt.Add;
            }
            if (button == btnProductParDel)
            {
                fileOpt = FileOpt.Del;
            }
            _em.GetEvent<ProductEvent>().Publish(fileOpt);
        }

        private void btnFLowNew_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.Create);
        }

        private void btnFlowOpen_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.Open);
        }

        private void btnFlowSave_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.Save);
        }

        private void btnFLowSaveAs_Click(object sender, EventArgs e)
        {
            _em.GetEvent<FlowEvent>().Publish(FileOpt.SaveAs);
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            _em = EventMgr.Ins;
            _pagesMgr = PagesMgr.Ins;
            _container = UnityIoc.Ins.Container;
            _pagesMgr.RegisterPanel(panMain);

            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmAuto"), "FrmAuto");
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmManualDebug"), "FrmManualDebug");
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmProductMgr"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmFlowEdit"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmStationDebug"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmIODebug"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmMotionConfig"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmIOConfig"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmStationConfig"));
            _pagesMgr.RegisterPageToPanel(panMain.Name, _container.Resolve<UIPage>("FrmDeviceConfig"));

            _pagesMgr.ShowPage(panMain.Name, "FrmAuto");

            if (Settings.Default.width > 0) Width = Settings.Default.width;
            if (Settings.Default.height > 0) Height = Settings.Default.height;
            this.Top = Settings.Default.top;
            this.Left = Settings.Default.left;
        }

        private void tabRibbon_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPage.Tag == null) return;
            string page = e.TabPage.Tag.ToString();
            _pagesMgr.ShowPage(panMain.Name, page);
        }

        private void ShowPage(object sender, EventArgs e)
        {
            Control control = sender as Control;
            _pagesMgr.ShowPage(panMain.Name, control.Tag.ToString());
        }

        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {

            var result = MessageBox.Show("是否保存修改？", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            switch (result)
            {
                case DialogResult.Yes:
                    _em.GetEvent<FlowEvent>().Publish(FileOpt.Save);
                    MotionMgr.Ins.Save();
                    IOMgr.Ins.Save();
                    StationsMgr.Ins.Save();
                    break;
                case DialogResult.No:
                    //退出，不保存
                    break;
                case DialogResult.Cancel:
                    e.Cancel = true;
                    return;
                    break;
            }

            _em.GetEvent<ExitApp>().Publish();
            Stopwatch sw = Stopwatch.StartNew();
            while (sw.ElapsedMilliseconds < 1000)
            {
                Thread.Sleep(100);
                Application.DoEvents();
            }

        }

        private void 工具箱ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            _em.GetEvent<ShowIdeWindow>().Publish(item.Text);
        }

        private void 默认布局ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            _em.GetEvent<ShowIdeWindow>().Publish(item.Text);
        }

        private void btnWindows_Click(object sender, EventArgs e)
        {
            btnWindows.ShowContextMenuStrip(leftButtonMenu, 0, btnWindows.Height);
        }

        private void 资源管理器ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            _em.GetEvent<ShowIdeWindow>().Publish(item.Text);
        }

        private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            _em.GetEvent<ShowIdeWindow>().Publish(item.Text);
        }

        private void 调试ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            _em.GetEvent<ShowIdeWindow>().Publish(item.Text);
        }

        private void 参数ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            _em.GetEvent<ShowIdeWindow>().Publish(item.Text);
        }

        private void 变量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = sender as ToolStripMenuItem;
            _em.GetEvent<ShowIdeWindow>().Publish(item.Text);
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
          Settings.Default.top=Top;
          Settings.Default.left=Left;
          Settings.Default.width=Width;
          Settings.Default.height=Height;
          Settings.Default.Save();
        }
    }
}